<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>品牌管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <!--bootstrap-toastr-->
    <script type="text/javascript" src="/bootstrap/toastr/toastr.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/toastr/toastr.css">

    <script>

        $(function () {
            getListBrands();
        });
        function getListBrands() {
            $.ajax({
                url : "/brand/getListBrands",
                dataType : "json",
                type : "post",
                data : {pageNumber : $("#pageNumber").val()},
                success : function (returnData) {
                    var result = returnData.data.list;
                    var table = "";
                    for (var i = 0; i < result.length; i++) {
                        table += "<tr>"
                        table += "<td><input name='box' value='"+ result[i].id +"' type='checkbox'></td>"
                        table += "<td>" + result[i].id + "</td>"
                        table +=     "<td>" + result[i].name + "</td>"
                        table += "<td>" + result[i].firstChar + "</td>"
                        table += "<td class='text-center'>"
                        table += "<button onclick='toUpdateBrand("+ result[i].id +")' type='button' class='btn bg-olive btn-xs' data-toggle='modal' data-target='#editModal'>修改</button>"
                        table += "</td>"
                        table += "</tr>"
                    }

                    $("#listBrand").html(table);

                    $("#showCount").html(returnData.data.total)
                    $("#showPage").html(returnData.data.pages)

                    $("#totalPage").val(returnData.data.pages)
                    $("#totalCount").val(returnData.data.total)
                    $("#pageNumber").val(returnData.data.pageNumer)

                    var rowPages = ""
                    for (var j = 1;j <= returnData.data.pages;j++){
                        rowPages += "<li>"
                        rowPages += "<a href='javascript:changePageNumber("+ j +")'>"+ j +"</a>"
                        rowPages += "</li>"
                        $("#pages").html(rowPages)
                    }
                },
                error : function (returnData) {
                    toastr.error(returnData.message)
                }
            })
        }

        function getPage(flag) {
            var pageNumber = parseInt($("#pageNumber").val());
            var totalPage = parseInt($("#totalPage").val());

            if (flag == 1){
                if (pageNumber <= 1){
                    toastr.warning("已经是第一页");
                    return;
                }
                $("#pageNumber").val(1);
            }else if (flag == 2) {
                if (pageNumber <= 1){
                    toastr.warning("已经是第一页");
                    return;
                }
                $("#pageNumber").val(pageNumber - 1);
            }else if (flag == 3){
                if (pageNumber >= totalPage){
                    toastr.warning("已经是最后一页");
                    return;
                }
                $("#pageNumber").val(pageNumber + 1);
            } else if (flag == 4) {
                if (pageNumber >= totalPage){
                    toastr.warning("已经是最后一页");
                    return;
                }
                $("#pageNumber").val(totalPage);
            }
            getListBrands();
        }

        function changePageNumber(obj) {
            $("#pageNumber").val(obj);
            getListBrands();
        }

        function reload() {
            window.location.reload();
        }


        function addBrand() {
            $.ajax({
                url : "/brand/insertBrand",
                type : "post",
                data : $("#insertBrandForm").serialize(),
                dataType : "json",
                success : function (returnData) {
                    if (returnData.code == 200) {
                        toastr.success(returnData.message);
                    } else {
                        toastr.error(returnData.message);
                    }
                    setTimeout(function(){
                            reload();
                        },500
                    );
                },
                error : function (returnData) {
                    alert(returnData.message);
                }
            });
        }

        function toUpdateBrand(id) {
            $.ajax({
                url : "/brand/toUpdateBrand/"+id,
                type : "post",
                dataType : "json",
                success : function (returnData) {
                    if (returnData.code == 200){
                        var data = returnData.data;
                        $("#id").prop("value", id);
                        $("#name").prop("value", data.name);
                        $("#firstChar").prop("value", data.firstChar);
                    }else {
                        toastr.error(returnData.message);
                    }
                },
                error : function (returnData) {
                    alert(returnData.message);
                }
            });
        }

        // 修改
        function updateBrand() {
            $.ajax({
                url : "/brand/updateBrand",
                type : "post",
                data : $("#updateBrandForm").serialize(),
                dataType : "json",
                success : function (returnData) {
                    if (returnData.code == 200) {
                        toastr.success(returnData.message);
                    } else {
                        toastr.error(returnData.message);
                    }
                    setTimeout(function(){
                            reload();
                        },500
                    );
                },
                error : function (returnData) {
                    alert(returnData.message);
                }
            });
        }

        // 删除
        function deleteBrand() {
            if (confirm("确定要删除选中数据吗?")) {
                var names = $("[name=box]:checked");
                var ids = [];
                $(names).each(function (i, e) {
                    ids.push(e.value)
                });
                if (ids <= 0) {
                    toastr.warning("请选择要删除的品牌")
                    return;
                }
                $.ajax({
                    url : "/brand/deleteBatch",
                    type : "post",
                    data : {ids : ids},
                    dataType : "json",
                    success : function (returnData) {
                        if (returnData.code == 200) {
                            toastr.success(returnData.message);
                        } else {
                            toastr.error(returnData.message);
                        }
                        setTimeout(function(){
                            reload();
                            },500
                        );

                    },
                    error : function (returnData) {
                        alert(returnData.message);
                    }
                });
            } else {
                return;
            }
        }


    </script>
</head>
<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                    <div class="box-header with-border">
                        <h3 class="box-title">品牌管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBrand()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                         
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
                                  <thead>
                                      <tr>
                                          <th class="" style="padding-right:0px">
                                              <input id="selall" type="checkbox" class="icheckbox_square-blue">
                                          </th>
                                          <th class="sorting_asc">品牌ID</th>
                                          <th class="sorting">品牌名称</th>
                                          <th class="sorting">品牌首字母</th>
                                          <th class="text-center">操作</th>
                                      </tr>
                                  </thead>
                                  <tbody id="listBrand">

                                  </tbody>
			                  </table>
			                  <!--数据列表/-->
                        <div align="center">
                                <nav aria-label="Page navigation">
                                    <input type="hidden" id="pageNumber" name="pageNumber">
                                    <input type="hidden" id="totalPage">
                                    <div class="page_num_inf">
                                        <i></i> 共
                                        <span id="showPage"></span>页<span id="showCount"></span>条
                                    </div>
                                    <ul class="pagination">
                                        <li>
                                            <a href="javascript:getPage(1);" aria-label="Previous">
                                                <span aria-hidden="true">首页</span>
                                            </a>

                                            <a href="javascript:getPage(2);" aria-label="Previous">
                                                <span aria-hidden="true">&laquo;上一页</span>
                                            </a>
                                        </li>
                                    </ul>
                                    <ul class="pagination" id="pages">

                                    </ul>
                                    <ul class="pagination">
                                        <li>
                                            <a href="javascript:getPage(3);" aria-label="Next">
                                                <span aria-hidden="true">下一页&raquo;</span>
                                            </a>
                                            <a href="javascript:getPage(4);" aria-label="Previous">
                                                <span aria-hidden="true">尾页</span>
                                            </a>
                                        </li>
                                    </ul>
                                </nav>
                            </div>
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
         
<!-- 增加窗口 -->
<form id="insertBrandForm">
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myAddModalLabel">增加品牌</h3>
            </div>
            <div class="modal-body">
                <table class="table table-bordered table-striped"  width="800px">
                    <tr>
                        <td>品牌名称</td>
                        <td><input name="name"  class="form-control" placeholder="品牌名称" >  </td>
                    </tr>
                    <tr>
                        <td>首字母</td>
                        <td><input name="firstChar" class="form-control" placeholder="首字母">  </td>
                    </tr>
                 </table>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addBrand()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
          </div>
        </div>
    </div>
</form>

  <!-- 修改窗口 -->
  <form id="updateBrandForm">
      <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog" >
              <div class="modal-content">
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                      <h3 id="myUpdateModalLabel">品牌修改</h3>
                  </div>
                  <div class="modal-body">
                      <table class="table table-bordered table-striped"  width="800px">
                          <input type="hidden" id="id" name="id">
                          <tr>
                              <td>品牌名称</td>
                              <td><input id="name" name="name"  class="form-control" placeholder="品牌名称" >  </td>
                          </tr>
                          <tr>
                              <td>首字母</td>
                              <td><input id="firstChar" name="firstChar"  class="form-control" placeholder="首字母">  </td>
                          </tr>
                      </table>
                  </div>
                  <div class="modal-footer">
                      <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateBrand()">修改</button>
                      <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
                  </div>
              </div>
          </div>
      </div>
  </form>
</body>
</html>